/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var sealStatus;
	var carStatus;
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/carfile/query",
        			params:function(){
        				var subnav = widget.get("subnav");
        				var status = subnav.getValue("status");
        				if(status=="false"){
        					sealStatus = status;
        				}else{
        					carStatus = status;
        				};
        				return{
        					"seal":sealStatus,
        					"carStatus":carStatus,
        					fetchProperties:"pkCar,carType,carNumber,perNmber,maxBagNumber,carStatus,version,seal"
        				}
        			},
        			columns:[{
        				name:"carType",
        				label:"车辆型号"
        			},{
        				name:"carNumber",
        				label:"车牌号码"
        			},{
        				name:"perNmber",
        				label:"准乘人数"
        			},{
        				name:"maxBagNumber",
        				label:"最大行李数"
        			},{
        				name:"carStatus.value",
        				label:"车辆状态",
        				format:function(value,row){
        					if(row.seal){
        						if(row.carStatus){
        							return row.carStatus.value;
        						}else{
        							return "禁用";
        						}
        					}else{
        						return "禁用";
        					}
        				}
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"open",
        					text:"启用",
        					show:function(index,data,rowEle){
        						var flag = true;
        						if(data.carStatus!=null && (data.carStatus.key=="Free" || data.carStatus.key=="Running")){
        							flag = false;
        						}
        						if(data.seal==false){
        							flag = true;
        						}
        						if(flag){
        							return true;
        						}
							},
							handler:function(index,data,rowEle){
								var cParams = {
										pkCar: data.pkCar,
										version: data.version,
										carStatus:"Free",
										seal: true,
									};
								aw.saveOrUpdate("api/carfile/save",cParams,function(cParams){
									grid.refresh();
								});
							}
        				},{
							id:"close",
							text:"禁用",
							show:function(index,data,rowEle){
								var flag = true;
        						if(data.carStatus!=null && (data.carStatus.key=="Running")){
        							flag = false;
        						}
        						if(data.carStatus!=null && (data.carStatus.key=="Free")){
        							flag = true;
        						}
        						if(data.seal==false){
        							flag = false;
        						}
        						if(flag){
        							return true;
        						}
							},
							handler:function(index,data,rowEle){
								var cParams = {
									pkCar: data.pkCar,
									version: data.version,
									carStatus:" ",
									seal: false,
								};
							aw.saveOrUpdate("api/carfile/save",cParams,function(cParams){
								grid.refresh();
							});
							}
						},{
        					id:"edit",
        					text:"修改",
        					show:function(index,data,rowEle){
        						if(data.carStatus!=null &&(data.carStatus.key=="Running")){
        							return false;
        						}else{
        							return true;
        						}
							},
        					handler:function(index,data,rowEle){
        						widget.get("subnav").hide(["search","add","status"]);	
        						widget.get("form").setData(data);
        						widget.hide([".J-grid"]).show([".J-form"]);
        					}
        				},{
        					id:"delete",
        					text:"删除",
        					show:function(index,data,rowEle){
        						if(data.carStatus!=null && (data.carStatus.key=="Running")){
        							return false;
        						}else{
        							return true;
        						}
							},
        					handler:function(index,data,rowEle){
								aw.del("api/carfile/"+data.pkCar+"/delete",function() {
									grid.refresh();
								});	
							}
        				}]
        			},]
        		},
        		
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});
